package com.reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class ReflectionTest {
	public static void main(String[] args) throws Exception {
		Class cls = Class.forName("com.reflect.OrnekSinif");

		Field[] fields = cls.getDeclaredFields();
		for (Field field : fields) {
			System.out.println(field);
		}
		System.out.println("----------------------------");
		Method[] methods = cls.getDeclaredMethods();
		for (Method method : methods) {
			System.out.println(method);
		}
		System.out.println("----------------------------");
		Constructor[] constructors = cls.getDeclaredConstructors();
		for (Constructor constructor : constructors) {
			System.out.println(constructor);
		}

		Object obj = cls.newInstance();
		if (obj instanceof OrnekSinif) {
			OrnekSinif os = (OrnekSinif) obj;
			os.surname = "Korkmaz";
			os.age = 25;
			System.out.println(os.getNameAndSurname());
		}

		Field field = cls.getField("surname");
		field.set(obj, "Korkmaz2");
		System.out.println(field.get(obj));

		// Field nameField = cls.getField("name");
		// nameField.set(obj, "Ahmet");
		// System.out.println(nameField.get(obj));

		Method method = cls.getMethod("getNameAndSurname");
		Object returnValue = method.invoke(obj, null);
		System.out.println(returnValue);

		Class[] parameter = new Class[2];
		parameter[0] = Integer.TYPE;
		parameter[1] = Integer.TYPE;

		Method toplaMethod = cls.getMethod("topla", parameter);
		Object returnValue2 = toplaMethod.invoke(obj, 5, 15);
		System.out.println(returnValue2);

	}
}
